#include <bits/stdc++.h>

#define int ll

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define _rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; i--)
#define dl make_pair
#define dk(...) make_tuple(__VA_ARGS__)
#define ec first
#define fb second
#define de(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef __int128 i128;
typedef pair <int, int> pii;

int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch ^ 48);
		ch = getchar();
	}
	return x * w;
}

template <typename _Tp>
void print(_Tp x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) print(x / 10);
	putchar(x % 10 + 48);
}

const int MAXN = 5e5 + 5;
int n, a[MAXN], m;

namespace seg {

#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define mid ((L + R) >> 1)

	bool sel[MAXN << 2];
	int sum[MAXN << 2], pos[MAXN << 2];

	void pushup(int x) {
		sel[x] = sel[ls(x)] | sel[rs(x)];
		if (sum[ls(x)] < sum[rs(x)]) pos[x] = pos[ls(x)];
		else pos[x] = pos[rs(x)];
		sum[x] = min(sum[ls(x)], sum[rs(x)]);
	}
	
	void build(int x, int L, int R) {
		if (L == R) return sel[x] = 0, sum[x] = a[L], pos[x] = L, void();
		build(ls(x), L, mid);
		build(rs(x), mid + 1, R);
		pushup(x);
	}

	void modify(int x, int L, int R, int p) {
		if (L == R) return sel[x] = 1, void();
		if (p <= mid) modify(ls(x), L, mid, p);
		else modify(rs(x), mid + 1, R, p);
		pushup(x);
	}

	pii query(int x, int L, int R, int l, int r) {
		if (l <= L && R <= r) return dl(sum[x], pos[x]);
		pii res = dl(1e18, 0);
		if (l <= mid) {
			pii val = query(ls(x), L, mid, l, r);
			if (val.ec < res.ec) res = val;
		}
		if (r > mid) {
			pii val = query(rs(x), mid + 1, R, l, r);
			if (val.ec < res.ec) res = val;
		}
		return res;
	}

	bool noModify(int x, int L, int R, int l, int r) {
		if (l <= L && R <= r) return sel[x];
		int res = 0;
		if (l <= mid) res |= noModify(ls(x), L, mid, l, r);
		if (r > mid) res |= noModify(rs(x), mid + 1, R, l, r);
		return res;
	}
} // namespace seg

struct ar {
	int l, r;
} qs[MAXN];

vector <ar> f;

signed main() {
#ifndef LOCAL
	freopen("gonna.in", "r", stdin);
	freopen("gonna.out", "w", stdout);
#endif
	n = read();
	rep (i, 1, n) a[i] = read();
	seg::build(1, 1, n);
	m = read();
	rep (i, 1, m) {
		qs[i].l = read(), qs[i].r = read();
	}
	sort(qs + 1, qs + 1 + m, [](ar a, ar b) {
		return (a.l == b.l) ? (a.r < b.r) : (a.l < b.l);	
	});
	int lst = 0;
	rep (i, 1, m) {
		if (qs[i].l == lst) continue;
		lst = qs[i].l;
		while (f.size() && f.back().r >= qs[i].r) f.pop_back();
		f.push_back(qs[i]);
	}
	int top = 0, ans = 0;
	for (int i = 1; i < f.size(); i++) {
		int aL = f[i].l, aR = f[top].r;
		if (aL > aR) {
			auto val = seg::query(1, 1, n, f[i - 1].l, f[top].r);
			seg::modify(1, 1, n, val.fb);
			ans += val.ec;
			top = i;
			continue;
		}
		int pL = f[i - 1].l;
		if (seg::query(1, 1, n, pL, aR).ec + seg::query(1, 1, n, f[i].l, f[i].r).ec < seg::query(1, 1, n, aL, aR).ec) {
			auto val = seg::query(1, 1, n, pL, aR);
			seg::modify(1, 1, n, val.fb);
			ans += val.ec;
			top = i;
			continue;
		}
	}
	auto val = seg::query(1, 1, n, f[f.size() - 1].l, f[top].r);
	seg::modify(1, 1, n, val.fb);
	ans += val.ec;
	print(ans);
	return 0;
}
